package cms.manage.user;

import cms.bean.payment.PaymentLog;
import cms.bean.platformShare.TopicUnhidePlatformShare;
import cms.bean.topic.HideTagType;
import cms.bean.topic.Topic;
import cms.bean.topic.TopicUnhide;
import cms.bean.user.AccessUser;
import cms.bean.user.PointLog;
import cms.bean.user.User;
import cms.manage.TopicManage;
import cms.service.setting.SettingService;
import cms.service.topic.TopicService;
import cms.web.action.payment.PaymentManage;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

import static cms.common.SystemConstant.AmountState.AMOUNT_IN;
import static cms.common.SystemConstant.AmountState.AMOUNT_OUT;
import static cms.common.SystemConstant.PaymentModule.PAYMENT_HIDDEN_TOPIC;
import static cms.common.SystemConstant.PaymentModule.PAYMENT_UNLOCK_CONTENT;

/**
 * 用户账号使用管理
 */
@Component("accountManage")
public class AccountManage {
    @Resource
    PointManage pointManage;
    @Resource
    SettingService settingService;
    @Resource UserManage userManage;
    @Resource
    TopicService topicService;
    @Resource
    TopicManage topicManage;
    @Resource
    PaymentManage paymentManage;

    public void use(HideTagType tagType, TopicUnhide topicUnhide, AccessUser accessUser, Topic topic) {
        switch (tagType){
            case GRADE:
                break;
            case PASSWORD://密码可见
                usePassword(topicUnhide,accessUser,topic);
                break;
            case POINT://积分购买可见
                usePoint(topicUnhide,accessUser,topic);
                break;
            case AMOUNT:{//余额购买可见
                useAmount(topicUnhide,accessUser,topic);
                break;
            }
            default:
        }
    }

    private void usePassword(TopicUnhide topicUnhide, AccessUser accessUser, Topic topic) {
        //保存'话题取消隐藏'
        topicService.saveTopicUnhide(topicManage.createTopicUnhideObject(topicUnhide),topicUnhide.getHideTagType(),
                null,accessUser.getUserName(),null,topic.getUserName(),null,
                topicUnhide.getAmount(),null,null,null,null);

    }

    /**
     * 使用余额
     * @param topicUnhide
     * @param accessUser
     * @param topic
     */
    private void useAmount(TopicUnhide topicUnhide, AccessUser accessUser, Topic topic) {
        Integer topicUnhidePlatformShareProportion = settingService.findSystemSetting().getTopicUnhidePlatformShareProportion();//平台分成比例
        //平台分成金额
        BigDecimal postUserNameShareAmount = null;
        BigDecimal platformShareAmount = null;
        if(topicUnhidePlatformShareProportion >0){
            //平台分成金额 = 总金额 * (平台分成比例 /100)
            platformShareAmount = topicUnhide.getAmount().multiply(new BigDecimal(String.valueOf(topicUnhidePlatformShareProportion)).divide(new BigDecimal("100")));
            postUserNameShareAmount = topicUnhide.getAmount().subtract(platformShareAmount);
        }else{
            postUserNameShareAmount = topicUnhide.getAmount();
        }

        Date time = new Date();
        PaymentLog paymentLog = new PaymentLog();
        paymentLog.setPaymentRunningNumber(paymentManage.createRunningNumber(accessUser.getUserId()));//支付流水号
        paymentLog.setPaymentModule(PAYMENT_HIDDEN_TOPIC.getValue());//支付模块 7.余额购买话题隐藏内容
        paymentLog.setSourceParameterId(String.valueOf(topic.getId()));//参数Id
        paymentLog.setOperationUserType(2);//操作用户类型  0:系统  1: 员工  2:会员
        paymentLog.setOperationUserName(accessUser.getUserName());//操作用户名称  0:系统  1: 员工  2:会员
        paymentLog.setAmountState(AMOUNT_OUT.getValue());//金额状态  1:账户存入  2:账户支出
        paymentLog.setAmount(topicUnhide.getAmount());//金额
        paymentLog.setInterfaceProduct(0);//接口产品
        paymentLog.setUserName(accessUser.getUserName());//用户名称
        paymentLog.setTimes(time);

        Object consumptionPaymentLogObject = paymentManage.createPaymentLogObject(paymentLog);

        Object incomePaymentLogObject = null;
        TopicUnhidePlatformShare topicUnhidePlatformShare = null;
        if(!topic.getIsStaff()){//如果是用户
            User user = userManage.queryCacheFindUserByUserName(topic.getUserName());
            String paymentRunningNumber = paymentManage.createRunningNumber(user.getId());//支付流水号
            PaymentLog incomePaymentLog = new PaymentLog();
            incomePaymentLog.setPaymentRunningNumber(paymentRunningNumber);//支付流水号
            incomePaymentLog.setPaymentModule(PAYMENT_UNLOCK_CONTENT.getValue());//支付模块 80.余额购买话题隐藏内容分成收入
            incomePaymentLog.setSourceParameterId(String.valueOf(topic.getId()));//参数Id
            incomePaymentLog.setOperationUserType(2);//操作用户类型  0:系统  1: 员工  2:会员
            incomePaymentLog.setOperationUserName(accessUser.getUserName());//操作用户名称  0:系统  1: 员工  2:会员
            incomePaymentLog.setAmountState(AMOUNT_IN.getValue());//金额状态  1:账户存入  2:账户支出
            incomePaymentLog.setAmount(postUserNameShareAmount);//金额
            incomePaymentLog.setInterfaceProduct(0);//接口产品
            incomePaymentLog.setUserName(topic.getUserName());//用户名称
            incomePaymentLog.setTimes(time);
            incomePaymentLogObject = paymentManage.createPaymentLogObject(incomePaymentLog);

            if(topicUnhidePlatformShareProportion >0){
                //平台分成
                topicUnhidePlatformShare = new TopicUnhidePlatformShare();
                topicUnhidePlatformShare.setTopicId(topic.getId());//话题Id
                topicUnhidePlatformShare.setStaff(topic.getIsStaff());//分成用户是否为员工
                topicUnhidePlatformShare.setPostUserName(topic.getUserName());//发布话题的用户名称
                topicUnhidePlatformShare.setUnlockUserName(accessUser.getUserName());//解锁话题的用户名称
                topicUnhidePlatformShare.setPlatformShareProportion(topicUnhidePlatformShareProportion);//平台分成比例
                topicUnhidePlatformShare.setPostUserShareRunningNumber(paymentRunningNumber);//发布话题的用户分成流水号
                topicUnhidePlatformShare.setTotalAmount(topicUnhide.getAmount());//总金额
                topicUnhidePlatformShare.setShareAmount(platformShareAmount);//平台分成金额
                topicUnhidePlatformShare.setUnlockTime(time);//解锁时间
            }

            //删除用户缓存
            userManage.deleteCacheFindUserById(user.getId());
            userManage.deleteCacheFindUserByUserName(topic.getUserName());
        }
        //保存'话题取消隐藏'
        topicService.saveTopicUnhide(topicManage.createTopicUnhideObject(topicUnhide),topicUnhide.getHideTagType(),
                null,accessUser.getUserName(),null,topic.getUserName(),null,
                topicUnhide.getAmount(),postUserNameShareAmount,consumptionPaymentLogObject,incomePaymentLogObject,topicUnhidePlatformShare);

    }

    private void usePoint(TopicUnhide topicUnhide, AccessUser accessUser, Topic topic) {
        Date time = new Date();
        PointLog pointLog = new PointLog();
        pointLog.setId(pointManage.createPointLogId(accessUser.getUserId()));
        pointLog.setModule(400);//400.积分购买隐藏话题
        pointLog.setParameterId(topic.getId());//参数Id
        pointLog.setOperationUserType(2);//操作用户类型  0:系统  1: 员工  2:会员
        pointLog.setOperationUserName(accessUser.getUserName());//操作用户名称
        pointLog.setPointState(2);//2:账户支出
        pointLog.setPoint(topicUnhide.getPoint());//积分
        pointLog.setUserName(accessUser.getUserName());//用户名称
        pointLog.setRemark("");
        pointLog.setTimes(time);
        //用户消费积分日志
        Object consumptionPointLogObject  = pointManage.createPointLogObject(pointLog);
        Object incomePointLogObject = null;
        if(!topic.getIsStaff()){//如果是用户
            User _user = userManage.queryCacheFindUserByUserName(topic.getUserName());
            PointLog incomePointLog = new PointLog();
            incomePointLog.setId(pointManage.createPointLogId(_user.getId()));
            incomePointLog.setModule(400);//400.积分购买隐藏话题
            incomePointLog.setParameterId(topic.getId());//参数Id
            incomePointLog.setOperationUserType(2);//操作用户类型  0:系统  1: 员工  2:会员
            incomePointLog.setOperationUserName(accessUser.getUserName());//操作用户名称
            incomePointLog.setPointState(1);//1:账户存入
            incomePointLog.setPoint(topicUnhide.getPoint());//积分
            incomePointLog.setUserName(topic.getUserName());//用户名称
            incomePointLog.setRemark("");
            incomePointLog.setTimes(time);
            //用户收入积分日志
            incomePointLogObject = pointManage.createPointLogObject(incomePointLog);
            //删除用户缓存
            userManage.deleteCacheFindUserById(_user.getId());
            userManage.deleteCacheFindUserByUserName(topic.getUserName());

        }
        //保存'话题取消隐藏'
        topicService.saveTopicUnhide(topicManage.createTopicUnhideObject(topicUnhide),topicUnhide.getHideTagType(),
                topicUnhide.getPoint(),accessUser.getUserName(),consumptionPointLogObject,topic.getUserName(),incomePointLogObject,
                null,null,null,null,null);
    }
}
